


-------怪物控制------

boss = {}
boss.bf = {}

-----计算 self 相对于 tar 的角度-----
local TarAngel = function (self,tar)
	local np = tar - self
	local angel = 20
	local da = np.x
	local db = np.z
	local dc = math.sqrt(da * da + db * db)
	if da > 0 and db < 0 then angel = math.asin(-db / dc) * 180 / math.pi + 90 end
	if da > 0 and db > 0 then angel = (math.asin(da/dc)) * 180 / math.pi end
	if db < 0 and da < 0 then angel = math.asin(-da/dc) * 180 / math.pi + 180 end
	if da < 0 and db > 0 then angel = (math.asin(db/dc) * 180 / math.pi ) + 270 end
	return Vector3.New(-90,angel,0)
end

------------攻击玩家----------
local function atkran(pos,delay,obj)
	local ran = {}
	ran.over = false
	ran.ball = WorkSpace.怪物们.ran:Clone()
	ran.ball.Position = pos
	ran.ball.Scale = Vector3.New(0,0.03,0)
	local rb = ran.ball:Clone()
	rb.Scale = Vector3.New(obj.damagerange,0.03,obj.damagerange)
	rb.Position = pos
	rb.Transparency = 0.3
	
	delay = delay or 5
	local t = 0
	local function gr(delta)
		if ran.over then return end
			
		t = t + delta
		local sl = t * obj.damagerange/delay	--范围4
		ran.ball.Scale = Vector3.New(sl,0.03,sl)
		if obj.isdead then
			GameRun.Update:DisConnect(gr)
			ran.ball:Destroy()
			rb:Destroy()
			return
		end
		
		if t > (delay) then
			GameRun.Update:DisConnect(gr)
			ran.ball:Destroy()
			rb:Destroy()
			
			obj.ball.Rotation = TarAngel(obj.ball.Position,obj.tarpos)
			obj.tarpos = nil			
			if obj.isdead then return; end
			
			local player = PlayerAvatar.getAva()
			if player then
				local dis = Vector3.Distance(pos,player.Position)
				--print(dis)
				--攻击范围
				if dis < obj.damagerange then
					local he = player.Health
					if (he - obj.attackdamage) > -10 then
						player.Health = he - obj.attackdamage			---攻击力
					end
				end
			end
			obj.stage = 'move'
		end		
	end
	GameRun.Update:Connect(gr)
end
--测试攻击范围
--atkran(WorkSpace.出生点.Position + Vector3.New(0,0.3,0))

local function FindPosInWorkeSpace(WorkeSpacerange)
	local carrorts = WorkeSpacerange:GetAllChild()
	local p = math.random(1,#carrorts)
	local MovingToPos = carrorts[p].Position + Vector3.New(0,0.2,0)
	return MovingToPos,carrorts[p]
end

function boss.bf:CreatBF(name,hp,speed,stayrange)
	local bf = {}
	bf.isdead = false
	bf.monname = name or '蝙蝠'
	bf.hp = hp or 10
	bf.movespeed = speed or 9
	bf.Stayrange = stayrange or '胡萝卜'
	
	local bornplace = Farm[stayrange]	--出生范围
	
	bf.damagerange = 4			--伤害圈大小
	bf.attackdelta = 3  		--攻击延迟
	bf.attackrrange = 3			--攻击触发范围
	bf.attackdamage = 10		---伤害值
	
	local startpos = FindPosInWorkeSpace(bornplace)
	bf.ball = monname[name]:Clone()
	bf.ball.Name = bf.monname
	bf.ball.IsCollisionCallBack = false
	bf.ball.UseGravity = false
	--print(tostring(bf.ball.say.hp.maxValue))
	bf.ball.say.hp.maxValue = hp or 10
	bf.ball.say.hp.Value = hp or 10
	bf.stage = 'move'
	
	function bf.setpos(pos)		bf.ball.Position = pos	end
	bf.setpos(startpos)
	
	function bf:say(mess)
		self.ball.say.tex.Text = mess 
		self.st = 0
		local function later(delta)
			self.st = self.st + delta
			if self.st > 2 then
				if self.ball and self.ball.say then
					self.ball.say.tex.Text = ''
				end
				GameRun.Update:DisConnect(later)
			end
		end
		GameRun.Update:Connect(later)
	end


	local function reborn()	------重生
		local t = 0
		local tim = WorkSpace.Timer:Clone()
		tim.Position = startpos
		local function reb(delta)
			t = t + delta
			tim.pan.tex.Text = tostring(math.floor(t))
			if(t > 60)then
				tim:Destroy()
				GameRun.Update:DisConnect(reb)
				bf.ball = monname[bf.monname]:Clone()
				bf.hp = hp or 10
				bf.ball.say.hp.Value = hp or 10
				bf.ball.say.hp.maxValue = hp or 10
				bf.isdead = false
				bf.setpos(startpos)
				bf.stage = 'move'
				GameRun.Update:Connect(localupdate)
			end		
		end
		GameRun.Update:Connect(reb)
	end
	
	function bf.gethurt(dam)
		bf.hp = bf.hp - dam
		bf:say('- '..dam)
		bf.ball.say.hp.Value = bf.hp
		if bf.hp < 1 then
			MessageEvent.FireClient(PlayerId,'Client',{msgid = 'BFdie',monname = bf.monname})
			bf.hp = 0
			bf.isdead = true
			local good = BuffMannager:Getgood()
			if good then
				good.ball.Position = bf.ball.Position + Vector3(0,0.5,0)
			end
			bf.ball:Destroy()
			Battle.State = 0
			Battle.Target = nil	
			GameRun.Update:DisConnect(localupdate)		
			reborn()			
			return;
		else
			bf.isdead = false
		end
	end
	
	
	local function AttackPla(self)		-------攻击
		if bf.isdead then return; end
		atkran(self.tarpos + Vector3.New(0,0.1,0),self.attackdelta,self)	
		self.stage = 'atk'
	end

	local function Tirggermove(self,delta)
		
		local player = PlayerAvatar.getAva()
		if player and player.Position and self.Stayrange == PlayerAvatar.getstate()  then
			self.tarpos = player.Position
			
			---------------------全局进攻对象-----------------
			Battle.State = 1
			Battle.Target = self
		else
			if Battle.Target and Battle.Target.monname == self.monname then
				Battle.Target = nil
				Battle.State = 0
			end
			return;
		end
		
		
		if self.ball and self.tarpos then
			local tar = self.tarpos
			local dis = Vector3.Distance(tar,self.ball.Position)
			--print
			if dis > self.attackrrange and self.stage == 'move' and PlayerAvatar.getstate() == bf.Stayrange then
				local np = tar - self.ball.Position
				local sp = Vector3.Normalize(np) * self.movespeed * delta
				
				self.setpos(self.ball.Position + sp)
				self.ball.Rotation = TarAngel(self.ball.Position,self.tarpos)
			elseif dis <= self.attackrrange and self.stage == 'move' and PlayerAvatar.getstate() == bf.Stayrange  then
				---------到达目标点-------
				AttackPla(self)
			end
		end
		
	end
	
	
	
	local up = 1
	local function updown(delta,bf)
		if bf.stage ~= 'move' then return end
		if up == 1 and bf.isdead == false then
			bf.ball.Position = bf.ball.Position + Vector3.New(0,1,0)*delta
		elseif up == 2 and bf.isdead == false then
			bf.ball.Position = bf.ball.Position - Vector3.New(0,1,0)*delta
		end
	
		if bf.isdead == false and bf.ball.Position.y > startpos.y + 1 then
			up = 2
		elseif bf.isdead == false and bf.ball.Position.y < startpos.y + 0.3 then
			up = 1
		end
	
	end

	local function localupdate(delta)
		if bf.isdead then return end
		updown(delta,bf)
		Tirggermove(bf,delta)
	end
	GameRun.Update:Connect(localupdate)
	
	return bf
end

--function boss.bf:CreatBF(名称,生命值,移动速度,(string)触发区域)
local bruce = boss.bf:CreatBF('蘑菇',25,2,'香菇田')
local Vanger = boss.bf:CreatBF('蝙蝠',20,3,'包菜田')
local miziha = boss.bf:CreatBF('三叶',20,3,'洋葱田')
--local Miao = boss.bf:CreatBF('小猫',20,3,'包菜田')
--local test = boss.bf:CreatBF('蝙蝠',25,2,'胡萝卜')

